var BSTMG = BSTMG || {};
BSTMG.supplier = {
		init: function(){
			var that = this;
			/*申请列表*/
			that.supplierListReq("","");
			/*代理商审核*/
			that.check();
			/*申请类型查询*/
			that.applyType();
			/*审核状态查询*/
			that.applyState();
			/*点击查询*/
			//that.query();
			/* 变更审核 */
			that.changeRule();			
		},
		changeRule: function(){
			var mPrivilege = $.trim($('#mPrivilege').html()),
				pPrivilege = $.trim($('#pPrivilege').html());
				applyType =$('#reApplyType').val();
			//alert(applyType); 
			var $update = $("#update");
			$update.on("click",function(){
				var html = '<div id="rules"><div class="input-group"><span class="input-group-addon">申请类型：</span>';
				html += '<select class="form-control" name="ApplyType" id="ApplyType">';
				
					if(applyType == 1) {
						html += '<option value="1" selected="selected">代理商申请</option><option value="2">市总代申请</option><option value="3">省总代申请</option><option value="4">软件开发商申请</option><option value="5">OEM商申请</option><option value="6">销售人员申请</option>';				
					}else if(applyType == 2) {
						html += '<option value="1">代理商申请</option><option value="2" selected="selected">市总代申请</option><option value="3">省总代申请</option><option value="4">软件开发商申请</option><option value="5">OEM商申请</option><option value="6">销售人员申请</option>';				
					}else if(applyType == 3) {
						html += '<option value="1">代理商申请</option><option value="2">市总代申请</option><option value="3" selected="selected">省总代申请</option><option value="4">软件开发商申请</option><option value="5">OEM商申请</option><option value="6">销售人员申请</option>';				
					}else if(applyType == 4) {
						html += '<option value="1">代理商申请</option><option value="2">市总代申请</option><option value="3">省总代申请</option><option value="4" selected="selected">软件开发商申请</option><option value="5">OEM商申请</option><option value="6">销售人员申请</option>';				
					}else if(applyType == 5) {
						html += '<option value="1">代理商申请</option><option value="2">市总代申请</option><option value="3">省总代申请</option><option value="4">软件开发商申请</option><option value="5" selected="selected">OEM商申请</option><option value="6">销售人员申请</option>';				
					}else if(applyType == 6) {
						html += '<option value="1">代理商申请</option><option value="2">市总代申请</option><option value="3">省总代申请</option><option value="4">软件开发商申请</option><option value="5">OEM商申请</option><option value="6" selected="selected">销售人员申请</option>';				
					}
				html += '</select></div><div class="input-group"><span class="input-group-addon">套餐优惠折扣：</span>';
				html += '<select class="form-control" name="mealPrivilege" id="mealPrivilege">';
				for(var i=10;i<=100;){
					if(mPrivilege == (i+"%")) {
						html += '<option value="'+i/100+'" selected="selected">'+i+'%</option>';
					}else {
						html += '<option value="'+i/100+'">'+i+'%</option>';
					}
					
					i = i+5;
				}
				html += '</select></div><div class="input-group"><span class="input-group-addon">增值应用折扣：</span>';
				html += '<select class="form-control" name="proPrivilege" id="proPrivilege">';
				for(var i=10;i<=100;){
					if(pPrivilege == (i+"%")){
						html += '<option value="'+i/100+'" selected="selected">'+i+'%</option>';
					}
					html += '<option value="'+i/100+'">'+i+'%</option>';
					i = i+5;
				}
				html += '</select></div><div>';
				var rule = BSTYUN.dialog.creat({
					id: "changerule",
					content: html,
					fixed : false,
					lock: false,
					button: [{
						name: "确定",
						callback: function(){
							//alert("确定回调函数")
							var supplierId = $('#supplierId').val(),
							mealPrivilege = $('#mealPrivilege').val(),
							proPrivilege = $('#proPrivilege').val(),
							applyType = $('#ApplyType').val();
						
							$.ajax({
								type : "POST",
								url : "/sysAdmin/operate/supplier/resetSupplier",
								async : false,
								data:{
									supplierId:supplierId,
									applyType : applyType,
									mealPrivilege : mealPrivilege,
									proPrivilege : proPrivilege,
								},
								success:function(data){
									if(data) {
										BSTYUN.dialog.ok("审核成功！",function(){
											location.href = "/sysAdmin/supplier/supplierDetail/"+supplierId;
										});
									}else {
										BSTYUN.dialog.warn("审核失败！");
									}
								}
							})
						},
						focus: true
					},{
						name: "取消",
						callback: function(){
							//alert("取消回调函数")
						},
						focus: false
					}]
				});
			});
		},
		supplierListReq: function(applyType,applyState){
			var url = "/sysAdmin/supplier/supplierAudit/search?applyType="+applyType+"&applyState="+applyState;
			BSTMG.common.hjhPages(url,function(data){
				var userList = data.userList;
				var $tbody = $("#supplierList tbody");
				var $tr = '';
				
				if(userList.length > 0) {
					for(var i=0;i<userList.length;i++){
						var userInfo = userList[i];
						
						var userEmailOrTell= userInfo.linkEmail;
						if(userEmailOrTell == null || userEmailOrTell == ''){
							userEmailOrTell = userInfo.linkTel;
						}
						$tr += '<tr class="odd">';
						$tr += '<td data-id="'+userInfo.supplierId+'">'+(i+1)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userEmailOrTell)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.realName)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.belongCompanyName)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(toDate(userInfo.addTime,"yyyy-MM-dd hh:mm:ss"))+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.provinceCodeName) + BSTMG.tools.checkNull(userInfo.cityCodeName) + BSTMG.tools.checkNull(userInfo.areaCodeName) + BSTMG.tools.checkNull(userInfo.address) + '</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(setApplyType(userInfo.applyType))+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(setApplyState(userInfo.applyState))+'</td>';
						if(userInfo.applyState == 0){
							$tr += '<td><a type="button"  class="btn btn-danger" href="/sysAdmin/supplier/supplierDetail/'+userInfo.supplierId+'">审核</a></td>';
						}else if(userInfo.applyState == 1||userInfo.applyState == 2){
							$tr += '<td><a type="button" class="btn btn-info" href="/sysAdmin/supplier/supplierDetail/'+userInfo.supplierId+'">详情</a></td>';
						}
						
						if(userInfo.applyState == 0||userInfo.applyState == 2){
							$tr += '<td><a type="button" class="btn btn-primary disabled" onclick="doSupplier('+userInfo.supplierId+',false);">无效</a></td>';
						}else if(userInfo.applyState == 1){
							if(userInfo.isValid == 1){
								$tr += '<td><a type="button" class="btn btn-primary" onclick="doSupplier('+userInfo.supplierId+',false);">停用</a></td>';
							}else if(userInfo.isValid == 0 || userInfo.isValid == null){
								$tr += '<td><a type="button" class="btn btn-success"  onclick="doSupplier('+userInfo.supplierId+',true);">启用</a></td>';
							}
						}
						
						$tr += '</tr>';
						
					}
					$tbody.html($tr);
				}else {
					var $tr = '<tr class="odd"><td colspan="10">暂无数据！</td></tr>';
					$tbody.html($tr);
				}
			});
		},
		/*代理商审核*/
		check : function(){
			/*审核通过*/
			$('#pass').on('click',function(){
				$('#applyState').val(1);
				toAudit();
			})
			/*审核退回*/
			$('#unpass').on('click',function(){
				var supplierId = $('#supplierId').val(),
				pstate = $('#pstate').val();
				$('#applyState').val(2);
				BSTMG.supplier.returnDialog("<textarea rows='5' cols='20' id='return_reason'></textarea>",toAudit);
			})
		},
		
		applyType : function(){
			$('#applyType').on('change',function(){
				var applyType = $('#applyType').val(),
				applyState = $('#applyState').val();
				BSTMG.supplier.supplierListReq(applyType,applyState);
			})
		},
		applyState : function(){
			$('#applyState').on('change',function(){
				var applyType = $('#applyType').val(),
				applyState = $('#applyState').val();
				BSTMG.supplier.supplierListReq(applyType,applyState);
			})
		},
		
		/*点击查询*/
//		query : function(){
//			$('#query').on('click',function(){
//				var applyType = $('#applyType').val(),
//				applyState = $('#applyState').val();
//				BSTMG.supplier.supplierListReq(applyType,applyState);
//			})
//		},
		
		
		returnDialog : function(html, callback) {
			var dialog = null, html = '<div class="warn-tip"><h4>退回理由：</h4>'
					+ html + "</div>";
			dialog = BSTYUN.dialog.creat({
				id : "return",
				content : html
			});
			dialog.button({
				name : "提交",
				focus : true,
				callback : callback
			})
		},
}

$(function(){
	BSTMG.supplier.init();
});

//设置申请类型
function setApplyType(para){
	if (para == "1") {
		return "代理商";
	} else if (para == "2") {
		return "市总代";
	} else if(para == "3"){
		return "省总代";
	} else if(para == "4"){
		return "软件开发商";
	} else if(para == "5"){
		return "OEM商合作";
	}else if(para == "6"){
		return "销售人员";
	}
}
//设置审核状态
function setApplyState(para){
	if (para == "0") {
		return "待审核";
	} else if (para == "1") {
		return "已通过";
	} else if(para == "2"){
		return "退回";
	} else if(para == "3"){
		return "无效";
	}
}
//审核操作
function toAudit(){
	var supplierId = $('#supplierId').val(),
		pstate = $('#pstate').val(),
		applyState = $('#applyState').val(),
		mealPrivilege = $('#mealPrivilege').val(),
		proPrivilege = $('#proPrivilege').val(),
		returnReason = $('#return_reason').val(),
		applyType = $('#applyType').val();
	if(pstate == 1){
		BSTYUN.dialog.warn('已审批！不能重复审批！');
		return;
	}else if(pstate == 2){
		BSTYUN.dialog.warn('已审批！不能重复审批！');
		return;
	}
	$.ajax({
		type : "POST",
		url : "/sysAdmin/operate/supplier/updateSupplier",
		async : false,
		data:{
			supplierId:supplierId,
			applyState:applyState,
			applyType : applyType,
			mealPrivilege : mealPrivilege,
			proPrivilege : proPrivilege,
			returnReason : returnReason
		},
		success:function(data){
			if(data) {
				BSTYUN.dialog.ok("审核成功！",function(){
					location.href = "/sysAdmin/supplier/supplierAudit";
				});
			}else {
				BSTYUN.dialog.warn("审核失败！");
			}
		}
	})
}
function doSupplier (supplierId,flag){
	var confirmContent = "";
	if(flag) {
		confirmContent = "确定要启用此代理商吗？";
	}else {
		confirmContent = "确定要停用此代理商吗？";
	}
	BSTYUN.dialog.confirm(confirmContent,function(){
		$.ajax({
			type : "POST",
			url : "/sysAdmin/operate/supplier/SupplierUpdateIsValid",
			async : false,
			data:{
				supplierId:supplierId,
				flag : flag
			},
			success:function(data){
				if(data.isSuccess){
					BSTMG.supplier.supplierListReq("","");
				}else {
					BSTYUN.dialog.warn(data.message);
				}
			}
		})
	});
}